<!-- 排班管理 -->
<template>
  <div class="dashboard-container transport-task customer-list-box">
    <div class="app-container">
      <!-- 搜索表单 -->
      <el-form
        class="searchForm"
        :model="searchForm"
        label-width="100px"
      >
        <el-row :gutter="60">
          <el-col :span="8">
            <el-form-item label="员工账号:">
              <el-input
                v-model="searchForm.employeeNumber"
                placeholder="请输入员工信息"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="员工姓名:">
              <el-input
                v-model="searchForm.name"
                placeholder="请输入员工姓名"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="工作模式:">
              <el-select
                v-model="searchForm.workPatternId"
                placeholder="请选择工作模式"
              >
                <el-option
                  v-for="item in selectWorkHistoryList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="月份:">
              <el-date-picker
                v-model="searchForm.month"
                type="month"
                value-format="yyyy-MM"
                placeholder="请选择月份"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="所属机构:"
            >
              <treeselect
                v-model="searchForm.agentId"
                :options="agencyOptions"
                :normalizer="normalizer"
                :searchable="true"
                style="width:100%"
                placeholder="请选择所属机构"
                no-options-text="暂无数据"
                no-results-text="暂无数据"
                @select="selectAgency"
                @open="open"
                @close="close"
              />
              <img
                ref="arrow"
                src="../../assets/pullDown.png"
                style="position:absolute;width:18px;height:18px;right:9px;top:12px;cursor:pointer;pointer-events:none;transition-duration:0.3s"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item>
              <el-button
                type="danger"
                @click="getWorkManage"
              >搜索</el-button>
              <el-button @click="resetForm">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <!-- 排班表格 -->
      <el-card class="table">
        <el-row style="margin-bottom:10px">
          <el-button
            type="warning"
            @click="$router.push('/transit/workArrange-setting')"
          >排班设置</el-button>
          <el-button @click="handleOpen">绑定排班</el-button>
        </el-row>
        <el-table
          :data="schedulingInfor"
          style="width: 100%"
          :header-cell-style="{'text-align':'center'}"
          :cell-style="{'text-align':'center'}"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            fixed
            type="selection"
            width="45"
          >
          </el-table-column>
          <el-table-column
            fixed
            prop="employeeNumber"
            label="员工账号"
            width="100"
          >
          </el-table-column>
          <el-table-column
            fixed
            prop="name"
            label="员工姓名"
            width="100"
          >
          </el-table-column>
          <el-table-column
            fixed
            prop="phone"
            label="员工电话"
            width="100"
          >
          </el-table-column>
          <el-table-column
            fixed
            prop="userType"
            label="员工角色"
            width="100"
          >
            <template v-slot="{row}">
              <span v-if="row.userType===1">员工</span>
              <span v-else-if="row.userType===2">快递员</span>
              <span v-else>司机</span>
            </template>
          </el-table-column>
          <el-table-column
            fixed
            prop="workPatternId"
            label="当前工作模式"
            width="120"
            align="right"
          >
            <template v-slot="{row}">
              <span>{{ judgmentWorkPatternById(row.workPatternId) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            v-for="index in length"
            :key="index"
            :label="index+''"
          >
            <template v-slot="{row}">
              <span
                v-if="!row.workPatternName"
                class="rest"
              >无</span>
              <span
                v-if="row.workPatternName && row.workSchedules[index-1] ===false"
                class="rest"
              >休</span>
              <span
                v-if="row.workPatternName && row.workSchedules[index-1] ===true"
                class="work"
              >上</span>
            </template>
          </el-table-column>
        </el-table>
        <el-row
          type="flex"
          justify="center"
        >
          <el-col :span="12">
            <el-pagination
              style="margin-top:30px"
              :current-page="searchForm.page"
              :page-sizes="[10, 15, 20, 25]"
              :page-size="searchForm.pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            >
            </el-pagination>
          </el-col>
        </el-row>
      </el-card>
    </div>
    <!-- 排班弹出层 -->
    <el-dialog
      title="人工调整"
      :visible.sync="showDialog"
      class="workArrange-manage-people-setting"
      @before-close="handleClose"
    >
      <el-form
        :model="workSchedulingsForm"
      >
        <el-form-item class="work-type">
          <el-radio
            v-model="workSchedulingsForm.workPatternType"
            label="1"
            @input="changeWorkPatternType"
          >礼拜制</el-radio>
          <el-radio
            v-model="workSchedulingsForm.workPatternType"
            label="2"
            @input="changeWorkPatternType"
          >连续制</el-radio>
        </el-form-item>
        <el-form-item
          label="模式名称:"
          label-width="120px"
        >
          <el-select
            v-model="workSchedulingsForm.workPatternId"
            placeholder="请选择模式"
          >
            <el-option
              v-for="item in selectWorkHistoryList1"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-row
          type="flex"
          justify="center"
        >
          <el-button
            style="width:100px"
            type="warning"
            @click="btnPeopleSetConfirm"
          >确认</el-button>
          <el-button
            size="mini"
            style="width:100px"
            @click="handleClose"
          >取消</el-button>
        </el-row>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import { selectWorkHistoryList, workManage, peopleSet } from '@/api/transit'
import { agencyList } from '@/api/institutions'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
  components: {
    Treeselect
  },
  data() {
    return {
      normalizer(node) {
        return {
          id: node.id,
          label: node.name,
          children: node.children
        }
      },
      searchForm: {
        page: 1, // 页码
        pageSize: 10, // 页面大小
        name: '', // 员工姓名
        employeeNumber: '', // 员工账号
        agencyId: null, // 所属机构
        month: '', // 月份
        workPatternId: '', // 工作模式ID 默认 0 ：无工作模式
        userType: '' // 用户类型：1：员工，2：快递员，3:司机
      },
      total: 0, // 排班信息总数
      selectWorkHistoryList: [], // 下拉工作列表
      selectWorkHistoryList1: [], // 下拉工作列表
      agencyOptions: [], // 所属机构树
      schedulingInfor: [], // 排班信息
      showDialog: false, // 绑定排班弹窗
      workSchedulingsForm: {
        userIdList: [], // 选择的员工id数组
        userType: '',
        workPatternId: '', // 工作模式ID
        workPatternType: '1' // 工作模式类型1:礼拜制，2：连续制
      },
      length: ''
    }
  },
  created() {
    this.length = new Date(new Date().getFullYear(), new Date().getMonth() + 1, 0).getDate()
  },
  mounted() {
    this.getWorkManage()
    this.getSelectWorkHistoryList()
    this.getAgencyList()
  },
  methods: {
    // 获取工作模式列表
    async getSelectWorkHistoryList() {
      try {
        const { data } = await selectWorkHistoryList()
        this.selectWorkHistoryList = [...data['1'], ...data['2']]
        if (this.workSchedulingsForm.workPatternType === '1') {
          this.selectWorkHistoryList1 = [...data['1']]
        } else {
          this.selectWorkHistoryList1 = [...data['2']]
        }
      } catch (error) {
        console.log(error)
      }
    },
    // 获取树状机构信息
    async getAgencyList() {
      try {
        const { data: res } = await agencyList()
        // 获取数据给options
        this.agencyOptions = JSON.parse(res)
      } catch (error) {
        console.log(error)
      }
    },
    // 赋予搜索id为搜索框内的数据
    selectAgency(val) {
      this.agencyOptions.agentId = val.id
    },
    // 打开所属机构下拉框
    open() {
      this.$refs.arrow.style.transform = 'rotate(-180deg)'
    },
    // 关闭所属机构下拉框
    close() {
      this.$refs.arrow.style.transform = 'rotate(0deg)'
    },
    // 获取排班信息
    async getWorkManage() {
      try {
        const { data } = await workManage(this.searchForm)
        this.schedulingInfor = data.items
        this.length = new Date(new Date().getFullYear(), new Date().getMonth() + 1, 0).getDate()
        this.total = +data.counts
      } catch (error) {
        console.log(error)
      }
    },
    // 重置表单
    resetForm() {
      this.searchForm = {
        page: 1, // 页码
        pageSize: 10, // 页面大小
        name: '', // 员工姓名
        employeeNumber: '', // 员工账号
        agencyId: '', // 所属机构
        month: '', // 月份
        workPatternId: '', // 工作模式ID 默认 0 ：无工作模式
        userType: '' // 用户类型：1：员工，2：快递员，3:司机
      }
      this.getWorkManage()
    },
    // 选择框
    handleSelectionChange(arr) {
      const newArr = []
      arr.forEach(item => {
        newArr.push(item.id)
      })
      this.workSchedulingsForm.userIdList = newArr
    },
    // 判断当前工作模式
    judgmentWorkPatternById(id) {
      const findWork = []
      this.selectWorkHistoryList.forEach(item => {
        if (item.id === id) {
          findWork.push(item)
        }
      })
      if (findWork.length === 0) {
        return '暂无安排'
      } else {
        const res = findWork[0]
        const name = res.name
        return name
      }
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val
      this.getWorkManage()
    },
    handleCurrentChange(val) {
      this.searchForm.page = val
      this.getWorkManage()
    },
    // 弹窗关闭
    handleClose() {
      this.showDialog = false
      this.workSchedulingsForm = {
        userIdList: [], // 选择的员工id数组
        userType: '',
        workPatternId: '', // 工作模式ID
        workPatternType: '1' // 工作模式类型1:礼拜制，2：连续制
      }
    },
    // 绑定排班单选按钮切换
    changeWorkPatternType() {
      this.getSelectWorkHistoryList()
    },
    // 绑定排班确认按钮
    async btnPeopleSetConfirm() {
      try {
        await peopleSet(this.workSchedulingsForm)
        this.getWorkManage()
        this.handleClose()
        this.$message.success('绑定成功')
      } catch (error) {
        console.log(error)
      }
    },
    handleOpen() {
      if (this.workSchedulingsForm.userIdList.length === 0) {
        this.$message.error('请选择排班员工')
        return 0
      } else {
        this.showDialog = true
      }
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
/deep/ th.date {
  .cell {
    padding-left: 15px !important;
  }
}
.work {
  width: 24px;
  height: 24px;
  background: #ffeeeb;
  border: 1px solid #e15536;
  border-radius: 50%;
  text-align: center;
  color: #e15536;
  font-size: 13px;
}
.history-name {
  width: auto !important;
  /deep/ .el-input__inner {
    width: 293px;
  }
}
.transport-task {
  padding: 10px 10px;
}
.searchForm {
  width: 100%;
  margin-top: -10px;
  padding: 20px 30px;
  border-radius: 10px;
  background-color: #fff;
}
.table{
  margin: 25px 0 50px;
  background-color: #fff;
}

.workArrange-manage-people-setting {
  /deep/ .el-dialog__body {
    padding: 30px 50px 10px 50px !important;
  }
  /deep/ .el-dialog__footer {
    padding-bottom: 40px;
  }
}
.borderwidth {
  width: 300px;
}

.work-type {
  /deep/ .el-form-item__content {
    margin-left: 40px !important;
  }
  /deep/ .el-radio:first-child {
    margin-right: 80px !important;
  }
  /deep/.el-radio__label {
    font-weight: bold;
    color: #333333;
  }
}

.dialog-footer {
  .el-button {
    width: 89px;
    height: 36px;
    padding: 10px 20px;
  }
}

.rest {
  width: 26px;
  height: 26px;
  background: #eff3f8;
  border: 1px solid #d8dde3;
  border-radius: 50%;
  text-align: center;
  color: #20232a;
  font-size: 12px;
}

.alert {
  margin: 10px 0px;
}
.pagination {
  margin-top: 40px;
}
.transport-task /deep/ .el-table td,
.el-table th {
  padding: 12px 0;
  min-width: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  text-overflow: ellipsis;
  vertical-align: middle;
  position: relative;
  text-align: left;
  overflow: hidden;
}

// 下拉框
/deep/ .el-select {
  width: 100%;
}

/*定义滚动条轨道 内阴影+圆角*/
/deep/ .el-table--scrollable-x .el-table__body-wrapper {
  padding-bottom: 5px;
  margin-bottom: 5px;
  &::-webkit-scrollbar {
    height: 10px;
  }

  /*定义滑块 内阴影+圆角*/
  &::-webkit-scrollbar-thumb {
    border-radius: 10px;
    background-color: rgba(144, 147, 153, 0.3);
  }

  &::-webkit-scrollbar-track-piece {
    margin-right: 3px;
    margin-left: 3px;
  }
}
/deep/ .el-card {
  overflow: initial;
}
</style>

<style></style>
